package com.atguigu.eduservice.controller.front;

import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/eduservice/teacherapi")
@Api(description = "讲师前台接口")
//@CrossOrigin
public class TeacherApiController {

    @Autowired
    private EduTeacherService teacherService;

    @Autowired
    private EduCourseService courseService;

    @ApiOperation("查询分页数据")
    @PostMapping("map/{curret}/{size}")
    public R getMap(@ApiParam(value = "当前页",name = "curret",required = true)@PathVariable long curret,
                    @ApiParam(value = "每页数",name = "size",required = true)@PathVariable long size)
    {
        Page<EduTeacher> page = new Page<>(curret,size);
        QueryWrapper<EduTeacher> query = new QueryWrapper<>();
        query.orderByDesc("id");
        teacherService.page(page,query);
        long total = page.getTotal();
        List<EduTeacher> list = page.getRecords();
        long pages = page.getPages();
        long sizes = page.getSize();
        boolean hasNext = page.hasNext();
        boolean hsaPrevious = page.hasPrevious();
        long currents = page.getCurrent();
        Map<String,Object> map = new HashMap<>();
        map.put("total",total);
        map.put("records",list);
        map.put("pages",pages);
        map.put("size",sizes);
        map.put("hasNext",hasNext);
        map.put("hasPrevious",hsaPrevious);
        map.put("current",currents);
        return R.ok().data("map",map);
    }

    @ApiOperation("根据id查询")
    @GetMapping("selectById/{id}")
    public R selectById(@PathVariable String id){
        EduTeacher eduTeacher = teacherService.getById(id);
        //根据id查询所有课程
        QueryWrapper<EduCourse> query = new QueryWrapper<>();
        query.eq("teacher_id",id);
        List<EduCourse> list = courseService.list(query);
        Map<String,Object> map = new HashMap<>();
        map.put("eduTeacher",eduTeacher);
        map.put("list",list);
        map.put("length",list.size());
        return R.ok().data("map",map);
    }

}
